package com.maxwell.googleplay.fragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseStream;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.maxwell.googleplay.R;
import com.maxwell.googleplay.adapter.AppItemAdapter;
import com.maxwell.googleplay.base.BaseFragment;
import com.maxwell.googleplay.base.LoadingPager;
import com.maxwell.googleplay.base.LoadingPager.LoadedResult;
import com.maxwell.googleplay.base.SuperBaseAdapter;
import com.maxwell.googleplay.bean.AppInfoBean;
import com.maxwell.googleplay.bean.HomeBean;
import com.maxwell.googleplay.conf.Constants.URL;
import com.maxwell.googleplay.factory.ListViewFactory;
import com.maxwell.googleplay.holder.BaseHolder;
import com.maxwell.googleplay.holder.HomeHolder;
import com.maxwell.googleplay.holder.PictureHolder;
import com.maxwell.googleplay.protocol.HomeProtocol;
import com.maxwell.googleplay.utils.UIUtils;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author yue
 * @创建时间 2016-3-30上午9:23:57
 * @描述 TODO
 * 
 */
public class HomeFragment extends BaseFragment {

	private List<AppInfoBean> mDatas;// ListVeiw的数据
	private List<String> mPictures;// 轮播图
	private HomeProtocol mProtocol;

	// 这个方法是在子线程中进行的，所以调用了sendSync（）异步方法
	@Override
	public LoadedResult initData() {
		/*try {
			// 发送网络请求
			HttpUtils httpUtils = new HttpUtils();
			String url = URL.BASEURL + "home";

			RequestParams params = new RequestParams();
			params.addQueryStringParameter("index", "0");
			ResponseStream responseStream = httpUtils.sendSync(HttpMethod.GET,
					url, params);
			String result = responseStream.readString();

			// 解析数据
			Gson gson = new Gson();
			HomeBean homeBean = gson.fromJson(result, HomeBean.class);
			LoadedResult state = checkState(homeBean);

			if (state != LoadedResult.SUCCESS) {
				return state;
			}
			state = checkState(homeBean.list);
			if (state != LoadedResult.SUCCESS) {
				return state;
			}

			mDatas = homeBean.list;
			pictures = homeBean.picture;
			return LoadedResult.SUCCESS;

		} catch (Exception e) {
			e.printStackTrace();
			return LoadedResult.ERROR;
		}
*/
		/*==========协议简单封装之后=============*/
		
		mProtocol = new HomeProtocol();
		try {
			HomeBean homeBean = mProtocol.loadData(0);
			LoadedResult state = checkState(homeBean);

			if (state != LoadedResult.SUCCESS) {
				return state;
			}
			state = checkState(homeBean.list);
			if (state != LoadedResult.SUCCESS) {
				return state;
			}

			mDatas = homeBean.list;
			mPictures = homeBean.picture;
		
			return LoadedResult.SUCCESS;
						
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return LoadedResult.ERROR;
		}
		
	}

	@Override
	public View initSuccessView() {
		//返回成功的视图	
		ListView listView = ListViewFactory.createListView();
		//创建一个PictureHolder
		PictureHolder pictureHolder = new PictureHolder();
		//触发加载数据
		pictureHolder.setDataAndRefreshHolderView(mPictures);
							
		View headView = pictureHolder.getHolderView();
		listView.addHeaderView(headView);
		// 设置adapter
		listView.setAdapter(new HomeAdapter(listView, mDatas));

		return listView;
	}

	class HomeAdapter extends AppItemAdapter{



		public HomeAdapter(AbsListView absListView, List<AppInfoBean> dataSource) {
			super(absListView, dataSource);
			// TODO Auto-generated constructor stub
		}



		@Override
		public List<AppInfoBean> onLoadMore() throws Exception {
			
			return loadMore(mDatas.size());

		}

		

		private List<AppInfoBean> loadMore(int index) throws Exception,
				IOException {
			/*// 真正加载更多数据
			// 发送网络请求
			HttpUtils httpUtils = new HttpUtils();
			String url = URL.BASEURL + "home";

			RequestParams params = new RequestParams();
			params.addQueryStringParameter("index", index + "");
			ResponseStream responseStream = httpUtils.sendSync(HttpMethod.GET,
					url, params);
			String result = responseStream.readString();

			// 解析数据
			Gson gson = new Gson();
			HomeBean homeBean = gson.fromJson(result, HomeBean.class);
			// LoadedResult state = checkState(homeBean);
			
			if (homeBean == null) {
				return null;
			}

			if (homeBean.list == null) {
				return null;
			}

			return homeBean.list;*/
			
			/*==========协议简单封装之后=============*/
			
			HomeBean homeBean = mProtocol.loadData(mDatas.size());
			
			
			if (homeBean == null) {
				return null;
			}

			if (homeBean.list == null) {
				return null;
			}

			return homeBean.list;
		}

	}
}
